package com.huang.springsecurityoauth2.config.security;

import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author HuangShen
 * @Description
 * @create 2021-01-12 10:59
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Resource(name = "passwordEncoder")
    private PasswordEncoder passwordEncoder;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // TODO: 2021/1/12  根据用户名去数据库查找 密码 角色信息

        System.out.println("用户名："+username);
        if (!"huangshen".equals(username)){
            throw  new UsernameNotFoundException("用户不存在");
        }

        String password = this.passwordEncoder.encode("123");


        return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
